package com.example.modulepicselector.adapter;

import android.content.Context;
import android.database.Cursor;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.modulepicselector.R;

import com.example.modulepicselector.engine.impl.ImageTanEngine;

import com.example.modulepicselector.internal.entity.Item;
import com.example.modulepicselector.internal.entity.SelectionSpec;
import com.example.modulepicselector.view.widget.CheckView;
import com.example.tanloadpic.TanLoadPic;

import org.jetbrains.annotations.NotNull;


import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import static com.example.modulepicselector.view.activity.MatActivity.sIsViewIdle;


public class PicAdapter extends RecyclerViewCursorAdapter<RecyclerView.ViewHolder>  {
    private WeakReference<Context> mContext;
    private ArrayList mTagNumList = new ArrayList();
    private int mCountIn = 1;


    private List<Item> selectItems = new ArrayList<>();
    private OnRecyclerViewItemClickListener mOnRecyclerViewItemClickListener;
    private final RecyclerView mRecyclerView;

    public PicAdapter(Context context, RecyclerView recyclerView) {
        super(null);
        mContext = new WeakReference<>(context);
        mRecyclerView = recyclerView;
    }

    @Override
    protected void onBindViewHolder(RecyclerView.ViewHolder viewHolder, Cursor cursor, int position) {
        RvViewHolder holder = (RvViewHolder) viewHolder;
        holder.itemView.setTag(position);
        holder.ivPic.setTag(position);
        if (position != cursor.getPosition()) {
            return;
        }
        final Item item = Item.valueOf(cursor);
        if (sIsViewIdle) {
            SelectionSpec.getInstance().imageEngine = new ImageTanEngine();
            SelectionSpec.getInstance().imageEngine.loadImage(mContext.get(), 1024,  (holder).ivPic, item.getUri());

        }


        if (selectItems.size() > 0) {

            holder.checkView.setCountable(SelectionSpec.getInstance().countable);
            if (SelectionSpec.getInstance().countable) {

                for (int i = 0; i < mTagNumList.size(); i++) {
                    int judge =(int) mTagNumList.get(i);
                    if (position == judge){
                        holder.checkView.setIsSelected(true);
                        holder.checkView.setCheckedNum(new ArrayList<>((mTagNumList)).indexOf((int) position) + 1);
                    }
                }
            }
        }

        holder.checkView.setOnClickListener(v -> {

            holder.checkView.setCountable(SelectionSpec.getInstance().countable);

            if (SelectionSpec.getInstance().countable) {
                int indexInQueue = 0;
                for (int i = 0; i < mTagNumList.size(); i++) {
                    int judge =(int) mTagNumList.get(i);
                    if (position == judge){
                        indexInQueue = i;
                    }
                }

                if (containItem(position)) {
                    holder.checkView.setIsSelected(false);
                    selectItems.remove(indexInQueue);
                    mTagNumList.remove(indexInQueue);
//                    notifyItemChanged(position);
                    for (int i = indexInQueue; i < mTagNumList.size(); i++) {


                        notifyItemChanged((int) mTagNumList.get(i));
                    }

                } else {
                    if (mTagNumList.size() < SelectionSpec.getInstance().maxSelectable) {
                        holder.checkView.setIsSelected(true);
                        mTagNumList.add(position);
                        selectItems.add(item);
                        holder.checkView.setCheckedNum(new ArrayList<>(selectItems).indexOf(item) + 1);
                    }
                }
            }

            if (mOnRecyclerViewItemClickListener!= null){
                mOnRecyclerViewItemClickListener.onItemClick(selectItems);
            }
        });
    }


    private boolean containItem(int position){
        for (int i = 0; i < mTagNumList.size(); i++) {
            int judge =(int) mTagNumList.get(i);
            if (position == judge){
                return true;
            }
        }
        return false;
    }


    @NonNull
    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_pic, parent, false);
        RvViewHolder holder = new RvViewHolder(view);

        return holder;
    }



    public static class RvViewHolder extends RecyclerView.ViewHolder {
        ImageView ivPic;
        public CheckView checkView;

        public RvViewHolder(View view) {
            super(view);
            ivPic = view.findViewById(R.id.iv_pic_list);
            checkView = view.findViewById(R.id.cv_check);
        }
    }


    /**
     * 对外暴露接口
     */
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(List<Item> selectItems);
    }


    /**
     * 对外暴露方法
     *
     * @param listener 接口
     */
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnRecyclerViewItemClickListener = listener;
    }






}
